<?php

class CalendarController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Lesson');
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }
        
    function indexAction()
    {
        $HTML = $month = '';
        
        if($this->_request->getParam('m')) {
            list($month,$year) = explode('_',$this->_request->getParam('m'));
            if($month > 0 & $month < 13) $month = (int)$month; else $month = NULL;
            if($year > 1900 & $year < 3000) $year = (int)$year; else $year = NULL;
        } 
        
        if(!$month || !$year) {
            $month = date('n');
            $year = date('Y');
        }    
        
        $HTML .= '<h2 style="text-align:center;">
                    <a style="text-decoration:none;" href="/calendar/index/m/'.date('n_Y',strtotime("-1 month",strtotime('15-'.$month.'-'.$year))).'")>&lt;&lt;</a> &nbsp; 
                    <a style="text-decoration:none;" href="/calendar/index/m/'.date('n_Y').'")>'.date('F Y',strtotime('15-'.$month.'-'.$year)).'</a> &nbsp; 
                    <a style="text-decoration:none;" href="/calendar/index/m/'.date('n_Y',strtotime("+1 month",strtotime('15-'.$month.'-'.$year))).'")>&gt;&gt;</a></h2>';
        $HTML .= $this->draw_calendar($month,$year);
            
        $this->view->table = $HTML;
        
        $this->render();
    }
    
    /* draws a calendar */
    function draw_calendar($month,$year){
      $Lesson = new Lesson();
      /* draw table */
      $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

      /* table headings */
      $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
      $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

      /* days and weeks vars now ... */
      $running_day = date('w',mktime(0,0,0,$month,1,$year));
      $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
      $days_in_this_week = 1;
      $day_counter = 0;
      $dates_array = array();

      /* row for week one */
      $calendar.= '<tr class="calendar-row">';

      /* print "blank" days until the first of the current week */
      for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        $days_in_this_week++;
      endfor;

      /* keep going with days.... */
      for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day">';
          /* add in the day number */
          $calendar.= '<div class="day-number">'.$list_day.'</div>';

          /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
          //$calendar.= str_repeat('<p>&nbsp;</p>',2);
          $calendar .= $Lesson->getLessonsText($year.'-'.$month.'-'.$list_day,$this->view->user->id,$this->view->user->authgroup_id);
          //$calendar.= $days_in_month.'/'.$month.'/'.$year;
          
        $calendar.= '</td>';
        if($running_day == 6):
          $calendar.= '</tr>';
          if(($day_counter+1) != $days_in_month):
            $calendar.= '<tr class="calendar-row">';
          endif;
          $running_day = -1;
          $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;
      endfor;

      /* finish the rest of the days in the week */
      if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
          $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
        endfor;
      endif;

      /* final row */
      $calendar.= '</tr>';

      /* end the table */
      $calendar.= '</table>';
      
      /* all done, return result */
      return $calendar;
    }
    
}